package com.example.teaching_server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import java.time.LocalDateTime;

/**
 * 主题内容实体类
 *
 * @author Teaching Server
 * @since 1.0.0
 */
@Getter
@Setter
@ToString(exclude = "theme")
@EqualsAndHashCode(of = "id")
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "theme_contents", indexes = {
        @Index(name = "idx_content_theme", columnList = "theme_id"),
        @Index(name = "idx_content_created", columnList = "created_at"),
        @Index(name = "idx_content_updated", columnList = "updated_at")
})
public class ThemeContent {

    /**
     * 内容ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 关联的主题
     */
    @NotNull(message = "主题不能为空")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "theme_id", nullable = false)
    private EducationTheme theme;

    /**
     * 内容标题
     */
    @NotBlank(message = "内容标题不能为空")
    @Size(max = 200, message = "内容标题长度不能超过200个字符")
    @Column(nullable = false, length = 200)
    private String title;

    /**
     * 内容描述
     */
    @Size(max = 1000, message = "内容描述长度不能超过1000个字符")
    @Column(length = 1000)
    private String description;

    /**
     * 封面图片
     */
    @Size(max = 500, message = "封面图片路径长度不能超过500个字符")
    @Column(name = "cover_image", length = 500)
    private String coverImage;

    /**
     * 内容正文（新增字段）
     */
    @Lob
    @Column(name = "content", columnDefinition = "TEXT")
    private String content;


    /**
     * 内容类型（新增字段）
     */
    @Size(max = 50, message = "内容类型长度不能超过50个字符")
    @Column(name = "content_type", length = 50)
    private String contentType;

    /**
     * 排序顺序（新增字段）
     */
    @Builder.Default
    @Column(name = "sort_order")
    private Integer sortOrder = 0;

    /**
     * 是否发布（新增字段）
     */
    @Builder.Default
    @Column(name = "is_published")
    private Boolean isPublished = false;

    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(name = "created_at", updatable = false)
    private LocalDateTime createdAt;

    /**
     * 更新时间
     */
    @UpdateTimestamp
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;


    /**
     * 验证内容是否有效
     */
    public boolean isValid() {
        return theme != null &&
                title != null && !title.trim().isEmpty();
    }

    /**
     * 内容是否已发布
     */
    public boolean isPublishedContent() {
        return Boolean.TRUE.equals(isPublished);
    }

    /**
     * 获取主题ID
     */
    public Long getThemeId() {
        return theme != null ? theme.getId() : null;
    }

    /**
     * 获取主题名称
     */
    public String getThemeName() {
        return theme != null ? theme.getName() : null;
    }

    /**
     * 内容摘要（前100字符）
     */
    public String getSummary() {
        if (description != null && !description.trim().isEmpty()) {
            return description.length() > 100 ?
                    description.substring(0, 100) + "..." : description;
        }
        return title.length() > 100 ? title.substring(0, 100) + "..." : title;
    }
}
